#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
    echo -e "${ERROR} ERROR ${RESET}${TEXT} You need to run this script as root!${RESET}"; exit 1
fi

function installPkg {
    clear
    read -p "Continue with the reinstallation? " _yesno
    if [[ "$_yesno" =~ ^[Yy]$ ]]; then
        if [[ ! -d /etc/shadowsocks-libev ]]; then
            wget -q -O - https://raw.githubusercontent.com/cybertize/doctype/default/packages/sslibev.sh | bash
        fi
        apt-get -y -qq --purge remove shadowsocks-libev &>/dev/null
        apt-get -y -qq autoremove &>/dev/null
        apt-get -qq autoclean &>/dev/null

        wget -q -O - https://raw.githubusercontent.com/cybertize/doctype/default/packages/sslibev.sh | bash &>/dev/null
    else
        echo
        echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; exit 1
        echo
    fi
}

function uninstallPkg {
    clear
    read -p "Are you sure you want to uninstall shadowsocks-libev? [Y/n] " _yesno
    if [[ "$_yesno" =~ ^[Yy]$ ]]; then
        apt-get -y -qq --purge remove shadowsocks-libev &>/dev/null
        apt-get -y -qq autoremove &>/dev/null
        apt-get -qq autoclean &>/dev/null

        echo -e "${SUCCESS} SUCCESS ${RESET}${TEXT} Purge & Removing shadowsocks-libev package ${RESET}"; exit 0
    else
        echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; exit 1
    fi
}

function configurePkg {
    clear
    server-default() {
        local OLD_PORT=$(cat /etc/shadowsocks-libev/server-default.json | grep '"server_port"' | cut -d ':' -f 2)

        read -p "Do you want to change port? " _yesno
        if [[ "$_yesno" == ^[Yy]$ ]]; then
            read -p "Enter new port: " DEFAULT_PORT
            if [[ -n "$DEFAULT_PORT" && "$DEFAULT_PORT" =~ ^[0-9]+$ ]]; then
                systemctl stop shadowsocks-libev-server@server-default
                sed -i "s/$OLD_PORT/$DEFAULT_PORT/g" /etc/shadowsocks-libev/server-default.json
                systemctl start shadowsocks-libev-server@server-default
            fi
        elif [[ "$_yesno" != ^[Yy]$ ]]; then
            echo -e "${YELLOW}WARNING:${RESET} Aborted..."; exit 1
        fi
    }
    server-stunnel() {
        local OLD_PORT=$(cat /etc/shadowsocks-libev/server-stunnel.json | grep '"server_port"' | cut -d ':' -f 2)

        read -p "Do you want to change port? " _yesno
        if [[ "$_yesno" =~ ^[Yy]$ ]]; then
            read -p "Enter new port: " STUNNEL_PORT
            if [[ -n "$STUNNEL_PORT" && "$STUNNEL_PORT" =~ ^[0-9]+$ ]]; then
                systemctl stop shadowsocks-libev-server@server-stunnel
                sed -i "s/$OLD_PORT/$STUNNEL_PORT/g" /etc/shadowsocks-libev/server-stunnel.json
                systemctl start shadowsocks-libev-server@server-stunnel
            fi
        else
            echo
            echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; exit 1
            echo
        fi
    }

    echo
    echo -e "${TEXT} SHADOWSOCKS-LIBEV CONFIG FILE MENU                         ${RESET}"
    echo
    echo -e "[01] ${GREEN}default${RESET}  - ${YELLOW}/etc/openvpn/server-default.conf${RESET}"
    echo -e "[02] ${GREEN}stunnel${RESET}  - ${YELLOW}/etc/openvpn/server-stunnel.conf${RESET}"
    echo -e "[03] ${GREEN}http${RESET}     - ${YELLOW}/etc/openvpn/obfs-http.conf${RESET}"
    echo -e "[04] ${GREEN}tls${RESET}      - ${YELLOW}/etc/openvpn/obfs-tls.conf${RESET}"
    echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
    while true; do
        read -p "Enter your choice: " _choice
        case "$_choice" in
            01|default)
                server-default && break
            ;;
            02|stunnel)
                server-stunnel && break
            ;;
            03|http)
                echo -e "${YELLOW}WARNING:${RESET} Sorry for now we cannot edit port for this particular configuration fail
                because this config file is in range port, each client use unique port."; break
            ;;
            04|tls)
                echo -e "${YELLOW}WARNING:${RESET} Sorry for now we cannot edit port for this particular configuration fail
                because this config file is in range port, each client use unique port."; break
            ;;
            *)
                echo; echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; echo
                configurePkg
            ;;
        esac
    done
}

function detailPkg {
    clear
    local unit_id=$(systemctl show shadowsocks-libev.service -p Id)
    local unit_name=$(systemctl show shadowsocks-libev.service -p Names)
    local unit_desc=$(systemctl show shadowsocks-libev.service -p Description)
    local is_active=$(systemctl is-active shadowsocks-libev.service)
    local is_enable=$(systemctl is-enabled shadowsocks-libev.service)

    echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} Shadowsocks service detail                                 ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "${YELLOW}     ID:${RESET} ${GREEN}$unit_id${RESET}"
    echo -e "${YELLOW}   Name:${RESET} ${GREEN}$unit_name${RESET}"
    echo -e "${YELLOW}   Desc:${RESET} ${GREEN}$unit_desc${RESET}"
    echo -e "${YELLOW} Status:${RESET} ${GREEN}$is_active & $is_enable${RESET}"
    # echo -e "${YELLOW}Listening:${RESET} ${GREEN}$squid_ports{RESET}";
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
    echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
}

function libevMenu {
    clear; echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} SHADOWSOKCS-LIBEV MENU                                     ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "[01] ${GREEN}reinstall${RESET}    - ${YELLOW}Reinstall shadowsocks-libev package${RESET}"
    echo -e "[02] ${GREEN}uninstall${RESET}    - ${YELLOW}Uninstall shadowsocks-libev package${RESET}"
    echo -e "[03] ${GREEN}configure${RESET}    - ${YELLOW}Edit/Configure shadowsocks-libev config${RESET}"
    echo -e "[04] ${GREEN}detail   ${RESET}    - ${YELLOW}Show shadowsocks-libev detail & status${RESET}"
    echo -e "[00] ${GREEN}quit     ${RESET}    - ${YELLOW}Exit from menu${RESET}"
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
    echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
    while true; do
        read -p "Enter your choice: " _choice
        case "$_choice" in
            01|reinstall)
                installPkg && break
            ;;
            02|uninstall)
                uninstallPkg && break
            ;;
            03|configure)
                configurePkg && break
            ;;
            04|detail)
                detailPkg && break
            ;;
            00|quit)
                exit 0
            ;;
            *)
                echo -e "${ERROR} ERROR ${RESET}${TEXT} Invalid option! ${RESET}"; libevMenu
            ;;
        esac
    done
}

if [[ -f /etc/shadowsocks-libev/server.conf ]]; then
    libevMenu
elif [[ ! -f /etc/shadowsocks-libev/server.conf ]]; then
    installPkg
fi
